home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #pragma once
- #include "OkAddress.h"
-
- //
- // There are five flavors of arrays:
- // struct Arrays (in which the contents of the elements are not looked at)
- // pointer Arrays (the elements are pointers to some memory)
- // object Arrays (the elements are objects, which must beructed and
- // destructed)
- //
- // Macros exist for each of these OkArray flavors:
- // OkDECLARE_Array (same as OkDECLARE_StructArray)
- // OkDECLARE_PtrArray
- // (acts like OkDECLARE_Array, except that pointers are
- // initialized to nil when new elements are added)
- // OkDECLARE_ObjArray
-
- class OkArray {
- public:
- u_int length();
- u_int elementSize()
- { return elementsize; };
- void resize(u_int length);
- void setMaxLength(u_int maxlength);
- void setIncrementSize(u_int insize);
- void qsort(u_int posn, u_int len);
- void qsort();
-
- void swap(u_int,u_int);
-
- virtual char *className() = 0;
-
- protected:
- OkArray(u_short esize, u_int initlength=0);
- OkArray(u_int esize, u_int num, void *data);
- OkArray(OkArray &);
- virtual ~OkArray();
-
- void* operator[](u_int index);
- void operator=(OkArray &);
-
- void append(void *item);
- void append(OkArray &);
- void remove(u_int start, u_int length=1);
- void insert(OkArray &, u_int posn);
- void insert(void *item, u_int posn);
-
- u_int find(void *, u_int start=0);
-
- // The objects in the array are stored sequentially at the
- // location pointed to by data. The length of the known
- // allocated segment is stored in maxi, in bytes. The
- // length of the array is stored in num, in *bytes*. The
- // size of an array element is stored in elementsize, in bytes.
-
- // data is allowed to be nil iff (maxi==0)
- OkAddress data;
-
- // num <= maxi
- u_int maxi,num;
- u_short elementsize;
- u_int incrementsize;
-
- // These two methods control how the array class goes to
- // fetch more memory.
- virtual void getmem();
- virtual void expand();
-
- // The raw methods are used to
- // implement methods which return an OkArray type.
- void * raw_copy();
- void * raw_extract(u_int start, u_int length);
- void * raw_cut(u_int start, u_int length);
- void * raw_head(u_int);
- void * raw_tail(u_int);
-
- void qsortInternal(u_int, u_int, void *);
- void destroy();
-
- // These three methods can be overridden to properly copy, delete,
- // and create new array elements in the desired manner. By default
- // `create' and `destroy' do nothing, and `copy' is a simple bcopy.
- //
- // The job of create is to take an area of uninitialized memory and
- // create a series of valid objects in it. The job of destroy is to
- // take a series of valid objects and destroy any resources they
- // consume. The status of the memory after the destroy is irrelevant.
- // The job of copy is to take a source array of objects, and copy
- // them to an area of *uninitialized* memory. There will not be any
- // objects stored there previous to the copy.
- virtual void createElements(void *, u_int numbytes);
- virtual void destroyElements(void *, u_int numbytes);
- virtual void copyElements(void *src, void *dst, u_int numbytes);
- virtual int compareElements(void *, void *) ;
- };
-
- #define OkArrayHeader(ARRAY,ITEM) \
- ARRAY(); \
- ARRAY(u_int size); \
- ARRAY(ARRAY&a); \
- ~ARRAY(); \
- virtual char* className(); \
- void operator=(ARRAY&a) { \
- maxi = a.maxi; num = a.num; if (data) delete (void*)data; \
- data = a.raw_copy(); } \
- ITEM & operator[](u_int index) \
- { assert(index>=0 && index*sizeof(ITEM) < num); \
- return *(ITEM *)((char*)((void*)data) + index * sizeof(ITEM));} \
- void append(ITEM & item) { OkArray::append(&item); } \
- void append(ARRAY & a) { OkArray::append(a); } \
- void remove(u_int start, u_int length=1) \
- { OkArray::remove(start,length); } \
- void removeAll() \
- { OkArray::remove(0, OkArray::length()); } \
- ARRAY cut(u_int start, u_int len = 1); \
- void insert(ARRAY & a, u_int p) \
- { OkArray::insert(a,p); } \
- void insert(ITEM & item, u_int p) \
- { OkArray::insert(&item,p);} \
- ARRAY extract(u_int start, u_int len); \
- ARRAY head(u_int len = 1); \
- ARRAY tail(u_int len = 1); \
- int find(ITEM& x, u_int start=0) { \
- return OkArray::find(&x,start); \
- } \
- protected: \
- ARRAY(u_int esize, u_int num, void *data); \
- public: \
- __enddef__
-
- #define OkArrayVirtuals \
- protected: \
- virtual void createElements(void *,u_int); \
- virtual void destroyElements(void *,u_int); \
- virtual void copyElements(void *,void*,u_int) ; \
- virtual int compareElements(void *, void *) ; \
- __enddef__
-
-
- //----------------------------------------------------------------------
- // Declare an array containing items of type ITEM.
-
- #define OkDECLARE_Array(ARRAY,ITEM) \
- class ARRAY : public OkArray { \
- public: \
- OkArrayHeader(ARRAY,ITEM) \
- }; \
- __enddef__
-
- #define OkDECLARE_StructArray(ARRAY,ITEM) OkDECLARE_Array(ARRAY,ITEM)
-
- #define OkDECLARE_ObjArray(ARRAY,ITEM) \
- class ARRAY : public OkArray { \
- public: \
- OkArrayHeader(ARRAY,ITEM) \
- OkArrayVirtuals \
- }; \
- __enddef__
-
- #define OkDECLARE_PtrArray(ARRAY, POINTER) \
- class ARRAY : public OkArray { \
- public: \
- OkArrayHeader(ARRAY,POINTER) \
- protected: \
- virtual void createElements(void *, u_int); \
- }; \
- __enddef__
-
- //----------------------------------------------------------------------
- // Various method implementations
-
- #define OkIMPLEMENT_ArrayMethods(ARRAY,ITEM) \
- ARRAY::ARRAY() : OkArray(sizeof(ITEM)) \
- { if (data) createElements(data,num); } \
- ARRAY::ARRAY(ARRAY& a) : OkArray(a.elementsize) \
- { maxi = a.maxi; num = a.num; data = a.raw_copy(); } \
- ARRAY::ARRAY(u_int size) : OkArray(sizeof(ITEM),size) \
- { createElements(data,num); } \
- ARRAY::~ARRAY() { destroy(); } \
- char* ARRAY::className() { return "ARRAY"; } \
- ARRAY ARRAY::cut(u_int start, u_int len) \
- {return ARRAY(sizeof(ITEM), len*sizeof(ITEM),raw_cut(start,len));}\
- ARRAY ARRAY::extract(u_int start, u_int len) \
- {return ARRAY(sizeof(ITEM), len*sizeof(ITEM),raw_extract(start,len));}\
- ARRAY ARRAY::head(u_int len) \
- {return ARRAY(sizeof(ITEM), len*sizeof(ITEM),raw_head(len));} \
- ARRAY ARRAY::tail(u_int len) \
- {return ARRAY(sizeof(ITEM),len*sizeof(ITEM),raw_tail(len));} \
- ARRAY::ARRAY(u_int esize, u_int num, void * data) \
- : OkArray(esize,num,data) {} \
- __enddef__
-
- #define OkIMPLEMENT_ObjArrayMethods(ARRAY,ITEM) \
- void ARRAY::createElements(void * start, u_int numbytes) { \
- ITEM * ptr = (ITEM *)start; \
- for (;;) { \
- if (numbytes == 0) break; \
- numbytes -= elementsize; \
- ITEM * obj = new(ptr) ITEM; \
- ptr++; \
- } \
- } \
- void ARRAY::destroyElements(void * start, u_int numbytes) { \
- ITEM * ptr = (ITEM *)start; \
- while (numbytes) { \
- numbytes -= elementsize; \
- ptr->ITEM::~ITEM(); \
- ptr++; \
- } \
- } \
- void ARRAY::copyElements(void * src, void * dst, \
- u_int numbytes) { \
- if (src<dst) { \
- src = (char*)src + numbytes; \
- dst = (char*)dst + numbytes; \
- ITEM * p = (ITEM *)src - 1; \
- ITEM * q = (ITEM *)dst - 1; \
- while (numbytes > 0) { \
- ITEM * obj = new(q--) ITEM(*p--); \
- numbytes -= elementsize; \
- } \
- } else { \
- ITEM * p = (ITEM *)src; \
- ITEM * q = (ITEM *)dst; \
- while (numbytes > 0) { \
- ITEM * obj = new(q++) ITEM(*p++); \
- numbytes -= elementsize; \
- } \
- } \
- } \
- int ARRAY::compareElements(void *o1, void *o2) { \
- return ((ITEM *)o1)->compare((ITEM *)o2); \
- } \
- __enddef__
-
- #define OkIMPLEMENT_PtrArrayMethods(ARRAY,POINTER) \
- void ARRAY::createElements(void * start, u_int numbytes) { \
- bzero(start,numbytes); \
- } \
- __enddef__
-
- //----------------------------------------------------------------------
- // Implement various types of arrays
-
- #define OkIMPLEMENT_Array(ARRAY,ITEM) \
- OkIMPLEMENT_ArrayMethods(ARRAY,ITEM) \
- __enddef__
-
- #define OkIMPLEMENT_StructArray(ARRAY,ITEM) \
- OkIMPLEMENT_ArrayMethods(ARRAY,ITEM) \
- __enddef__
-
- #define OkIMPLEMENT_ObjArray(ARRAY,ITEM) \
- OkIMPLEMENT_ArrayMethods(ARRAY,ITEM) \
- OkIMPLEMENT_ObjArrayMethods(ARRAY,ITEM) \
- __enddef__
-
- #define OkIMPLEMENT_PtrArray(ARRAY,POINTER) \
- OkIMPLEMENT_Array(ARRAY,POINTER) \
- OkIMPLEMENT_PtrArrayMethods(ARRAY,POINTER) \
- __enddef__
-